﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Softeem.control
{
    public partial class MyDataGridView : DataGridView
    {
        public MyDataGridView()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 奇数行
        /// </summary>
        private Color oddRowColor = Color.White;

        public Color OddRowColor
        {
            get { return oddRowColor; }
            set { oddRowColor = value; }
        }
        
        /// <summary>
        /// 偶数行
        /// </summary>
        private Color evenRowColor = Color.White;

        public Color EvenRowColor
        {
            get { return evenRowColor; }
            set { evenRowColor = value; }
        }
 

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);
            for (int i = 0; i < this.Rows.Count; i++)
            {
                if (i%2!=0)
                {
                    //奇数行
                    this.Rows[i].DefaultCellStyle.BackColor = this.OddRowColor;
                }
                else
                {
                    //偶数行
                    this.Rows[i].DefaultCellStyle.BackColor = this.EvenRowColor;
                }
            }
        }

        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            base.OnRowPostPaint(e);
            Font f = new Font("微软雅黑", 12);
            Rectangle rect = new Rectangle(e.RowBounds.X, e.RowBounds.Y, this.RowHeadersWidth, this.Rows[e.RowIndex].Height);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString((e.RowIndex + 1).ToString(), f, new SolidBrush(Color.Black), rect, sf);
        }
    }
}
